/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxBaseData
 * @Package com.rx.core.base
 * @Description: 数据流
 * @author: 陈锦韬
 * @date: 2021\6\9 0009
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.base;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.rx.core.data.UrlAnalyse;
import com.rx.core.dbase.*;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.enm.RX_DB_OPERATE;
import com.rx.core.enm.RX_VALID;
import com.rx.core.handler.RxHandlerController;
import com.rx.core.iface.RelationQuery;
import com.rx.core.iface.ReturnInputMode;
import com.rx.core.iface.TreeNode;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxContentHolder;
import com.rx.core.util.RxUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.Null;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @author: Administrator
 * @Description: 数据流
 * @date: 2021\6\9 0009
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Slf4j
@SuppressWarnings("unchecked")
public class RxBaseData extends RxEmpty{
    public static final String VAR_JSON_FILTER = "VAR_JSON_FILTER";
    public static final String VAR_JSON_REMOVE_FILTER = "VAR_JSON_REMOVE_FILTER";
    public static final String VAR_SQL_FILTER = "VAR_SQL_FILTER";
    public static final String VAR_SQL_REMOVE_FILTER = "VAR_SQL_REMOVE_FILTER";

    public static final String VAR_SQL_ONLY = "VAR_SQL_ONLY";
    /**
     * 多表查询时，不查第二张表，第三张表的字段。
     */
    public static final String VAR_SQL_SECOND_FILTER = "VAR_SQL_SECOND_FILTER";
    public static final String VAR_SQL_THREE_FILTER = "VAR_SQL_THREE_FILTER";
    /**
     * 页码
     */
    @JsonIgnore
    private transient Integer pageNum=0;
    /**
     * 页大小
     */
    @JsonIgnore
    private transient Integer pageSize=10;
    /**
     * 变态查询条件。不做序列化
     */
    @JsonIgnore
    private transient Map<String,Object> condition;

    /**
     * 返回类型。null 不处理。1-返回树形结构. 2-先分页。再以分页
     * 当用于关联查询时。详细设置看注解RxRelationInfo
     */
    @JsonIgnore
    private transient Object returnType;

    /**
     * 辅助 ********************************************************
     */
    @JsonIgnore
    private transient UrlAnalyse urlAnalyse;

    /**
     * 临时变量不做序列化
     */
    @JsonIgnore
    public transient Map<String,Object> tmpVariable;
    /**
     * 列表输入
     */
    @JsonIgnore
    private transient List<RxBaseData> dataList;

    /**
     * 最后返回
     */
    @JsonIgnore
    private transient Object resultData;

    @TableField(exist = false)
    protected List<TreeNode> children;
    /**
     * 错误信息
     */
    @JsonIgnore
    private transient String resultMessage;

    /**
     * 默认使用mybatis-plus。无须定义xml
     */
    @JsonIgnore
    private transient boolean useMybatisXml = false;
    /**
     * 处理器控制
     */
    @Null(message="不允许从前端传入处理器的控制信息")
    @JsonIgnore
    private transient RxHandlerController handlerController;

    /**
     * 是否把处理器结果记入tmpVariable。供下一个处理器处理
     */
    @JsonIgnore
    private transient boolean history = false;


    public RxBaseMapper databaseMapper(){
        // dao 已经在spring容器中，无需额外存储。
        return urlAnalyse.getMapper();
    }

    /**
     * 临时变量管理
     */
    private Map<String,Object> getTmpVariable(){
        if (EmptyChecker.isEmpty(tmpVariable)){
            tmpVariable = new HashMap<>();
        }
        return tmpVariable;
    }

    public Object getTmpVar(String key){
        return getTmpVariable().get(key);
    }

    public void setTmpVar(String key,Object object){
        getTmpVariable().put(key,object);
    }

    public <T> T getTmpVar(Class<T> cls){
        return (T)getTmpVariable().get(cls.getSimpleName());
    }

    public void setTmpVar(Object object){
        getTmpVariable().put(object.getClass().getSimpleName(),object);
    }

    /**
     * 临时加上过滤字段-不返回前端
     */
    public void appendFilter(List<String> fieldList){
        setTmpVar(VAR_JSON_FILTER,new ArrayList<>(fieldList));
    }
    /**
     * 临时删除过滤字段-返回前端
     */
    public void removeFilter(List<String> fieldList){
        setTmpVar(VAR_JSON_REMOVE_FILTER,fieldList);
    }

    /**
     * 临时加上过滤字段-不查询
     */
    public void appendSqlFilter(List<String> fieldList){
        setTmpVar(VAR_SQL_FILTER,new ArrayList<>(fieldList));
    }
    /**
     * 临时删除过滤字段-查询
     */
    public void removeSqlFilter(List<String> fieldList){
        setTmpVar(VAR_SQL_REMOVE_FILTER,fieldList);
    }

    /**
     * 临时加上。只查以下字段
     */
    public void onlySqlSelect(List<String> fieldList){
        setTmpVar(VAR_SQL_ONLY,new ArrayList<>(fieldList));
    }


    public void removeSecondTableField(){
        setTmpVar(VAR_SQL_SECOND_FILTER,VAR_SQL_SECOND_FILTER);
    }
    public void removeThreeTableField(){
        setTmpVar(VAR_SQL_THREE_FILTER,VAR_SQL_THREE_FILTER);
    }

    public void resetHandlerType(HANDLER_TYPE type){
        String formName = RxUtil.StringUtil.firstUpperHumpToDash(getClass().getSimpleName());
        setUrlAnalyse(UrlAnalyse.create(type.getUrlByMode(formName)));
        setHandlerController(RxHandlerController.create());
        getHandlerController().onlyDefault();
    }
    /**
     * 临时变量管理
     */
    public static RxBaseData create(Class cls, HANDLER_TYPE type) throws IllegalAccessException, InstantiationException {
        RxBaseData rxBaseData = (RxBaseData) cls.newInstance();
        String formName = RxUtil.StringUtil.firstUpperHumpToDash(cls.getSimpleName());
        rxBaseData.setUrlAnalyse(UrlAnalyse.create(type.getUrlByMode(formName)));
        rxBaseData.setHandlerController(RxHandlerController.create());
        return rxBaseData;
    }
    public static RxBaseData create(RxBaseData rxBaseData, HANDLER_TYPE type) throws IllegalAccessException, InstantiationException {
        String formName = RxUtil.StringUtil.firstUpperHumpToDash(rxBaseData.getClass().getSimpleName());
        rxBaseData.setUrlAnalyse(UrlAnalyse.create(type.getUrlByMode(formName)));
        rxBaseData.setHandlerController(RxHandlerController.create());
        return rxBaseData;
    }
    public static RxBaseData create(Class maincls,Class secondCls, HANDLER_TYPE type) throws IllegalAccessException, InstantiationException {
        RxBaseData rxBaseData = (RxBaseData) maincls.newInstance();
        String formName = RxUtil.StringUtil.firstUpperHumpToDash(maincls.getSuperclass().getSimpleName());
        String secondFormName = RxUtil.StringUtil.firstUpperHumpToDash(secondCls.getSimpleName());
        rxBaseData.setUrlAnalyse(UrlAnalyse.create(type.getUrlByMode(formName,secondFormName)));
        rxBaseData.setHandlerController(RxHandlerController.create());
        RxBaseData relation = (RxBaseData) secondCls.newInstance();
        RelationQuery relationQuery = (RelationQuery) rxBaseData;
        relationQuery.assignRelation(relation);
        return rxBaseData;
    }

    public static RxBaseData create(Class mainCls,Class secondCls,Class threeCls, HANDLER_TYPE type) throws IllegalAccessException, InstantiationException {

        String formName = RxUtil.StringUtil.firstUpperHumpToDash(mainCls.getSuperclass().getSimpleName());
        String secondFormName = RxUtil.StringUtil.firstUpperHumpToDash(secondCls.getSuperclass().getSimpleName());
        String threeFormName = RxUtil.StringUtil.firstUpperHumpToDash(threeCls.getSimpleName());

        RxBaseData rxBaseData = (RxBaseData) mainCls.newInstance();
        rxBaseData.setUrlAnalyse(UrlAnalyse.create(type.getUrlByMode(formName,secondFormName + "_" + threeFormName)));
        rxBaseData.setHandlerController(RxHandlerController.create());

        RxBaseData relation = (RxBaseData) secondCls.newInstance();
        RelationQuery relationQuery = (RelationQuery) rxBaseData;
        relationQuery.assignRelation(relation);

        RxBaseData relationThree = (RxBaseData) threeCls.newInstance();
        RelationQuery relationSecondQuery = (RelationQuery) relation;
        relationSecondQuery.assignRelation(relationThree);
        return rxBaseData;
    }

    /**
     * 在处理器内部指导如何进行下一步操作
     * skip 跳过
     * repeat 重复
     */
    public void skip(){
        if(EmptyChecker.isEmpty(handlerController)){
            handlerController = RxHandlerController.create();
        }
        handlerController.setSkipLength(1);
    }
    public void skip(Integer length){
        if(EmptyChecker.isEmpty(handlerController)){
            handlerController = RxHandlerController.create();
        }
        handlerController.setSkipLength(length);
    }
    public Integer skipLength(){
        if(EmptyChecker.isEmpty(handlerController)){
            return 0;
        }
        return handlerController.getSkipLength();
    }
    public void repeat(){
        if(EmptyChecker.isEmpty(handlerController)){
            handlerController = RxHandlerController.create();
        }
        handlerController.setRepeatLength(1);
    }
    public void repeat(Integer length){
        if(EmptyChecker.isEmpty(handlerController)){
            handlerController = RxHandlerController.create();
        }
        handlerController.setRepeatLength(length);
    }
    public Integer repeatLength(){
        if(EmptyChecker.isEmpty(handlerController)){
            return 0;
        }
        return handlerController.getRepeatLength();
    }
    public void reset(){
        if(EmptyChecker.notEmpty(handlerController)){
            handlerController.setSkipLength(0);
            handlerController.setRepeatLength(0);
        }
    }

    public QueryWrapper buildQueryWrapper(){
        QueryWrapper queryWrapper = buildQueryWrapperByCondition(false);
        if (this instanceof RxBaseDbMust){
            RxBaseDbMust rxBaseDbMust = (RxBaseDbMust) this;

            RxUtil.notNullDo(rxBaseDbMust.getBid(),e->queryWrapper.eq(RxBaseDbMust.BID,((String)e).trim()));
            RxUtil.nullDo(rxBaseDbMust.getValid(),()->rxBaseDbMust.setValid(RX_VALID.NORMAL.getCode()));
            queryWrapper.eq(RxBaseDbMust.VALID,rxBaseDbMust.getValid().trim());
            RxUtil.notNullDo(rxBaseDbMust.getVer(),e->queryWrapper.eq(RxBaseDbMust.VER,e));
        }
        if (this instanceof RxMarkDbMust){
            RxMarkDbMust rxBaseDbMust = (RxMarkDbMust) this;
            RxUtil.notNullDo(rxBaseDbMust.getCreateById(),e->queryWrapper.eq(RxMarkDbMust.CREATE_BY_ID,((String)e).trim()));
            RxUtil.notNullDo(rxBaseDbMust.getCreateDate(),e->queryWrapper.eq(RxMarkDbMust.CREATE_DATE,e));
            RxUtil.notNullDo(rxBaseDbMust.getUpdateById(),e->queryWrapper.eq(RxMarkDbMust.UPDATE_BY_ID,((String)e).trim()));
            RxUtil.notNullDo(rxBaseDbMust.getUpdateDate(),e->queryWrapper.eq(RxMarkDbMust.UPDATE_DATE,e));
        }
        if (this instanceof RxBizDbMust){
            RxBizDbMust rxBaseDbMust = (RxBizDbMust) this;
            RxUtil.notNullDo(rxBaseDbMust.getCreateBy(),e->queryWrapper.eq(RxBizDbMust.CREATE_BY,((String)e).trim()));
            RxUtil.notNullDo(rxBaseDbMust.getUpdateBy(),e->queryWrapper.eq(RxBizDbMust.UPDATE_BY,((String)e).trim()));
            RxUtil.notNullDo(rxBaseDbMust.getCreateOrgBy(),e->queryWrapper.eq(RxBizDbMust.CREATE_ORG_BY,((String)e).trim()));
            RxUtil.notNullDo(rxBaseDbMust.getCreateOrgId(),e->queryWrapper.eq(RxBizDbMust.CREATE_ORG_ID,((String)e).trim()));
        }

        List<Field> fieldList = RxUtil.Reflection.getFieldList(this.getClass(), TableField.class);
        for (Field field : fieldList) {
            // 非空赋值。
            RxUtil.notNullDo(RxUtil.Reflection.getFieldValue(this,field),
                    e->{
                        if (e instanceof String){
                            queryWrapper.eq(RxUtil.StringUtil.humpToLine(field.getName()),((String)e).trim());
                        }else{
                            queryWrapper.eq(RxUtil.StringUtil.humpToLine(field.getName()),e);
                        }
                    });
        }
        return queryWrapper;
    }

    /**
     * 可以从处理器设置查询条件
     * @param queryWrapper
     */
    public void setQueryWrapper(QueryWrapper queryWrapper){
        setTmpVar(RxBaseConstant.RESULT_QUERY_KEY,queryWrapper);
    }

    /**
     * 获取查询条件。注意函数不要用get开头，有可能被序列化。
     * @return
     */
    public QueryWrapper queryWrapper(boolean yesUpdate){
        QueryWrapper wrapper = (QueryWrapper)getTmpVar(RxBaseConstant.RESULT_QUERY_KEY);
        if(EmptyChecker.isEmpty(wrapper)){
            if(yesUpdate){
                wrapper = buildQueryWrapperByCondition(yesUpdate);
            }else {
                wrapper = buildQueryWrapper();
            }
        }
        // 字段只显示
        List<String> list = (List<String>) getTmpVar(VAR_SQL_ONLY);
        if(EmptyChecker.notEmpty(list)){
            wrapper.select(list.toArray(new String[list.size()]));
        }

        return wrapper;
    }

    /**
     * 用于统计，经过特殊处理
     * @return
     */
    public QueryWrapper queryWrapperForStatis(){
        QueryWrapper wrapper = (QueryWrapper)getTmpVar(RxBaseConstant.RESULT_QUERY_KEY);
        if (EmptyChecker.isEmpty(wrapper)){
           return buildQueryWrapper();
        }
        return wrapper;
    }

    /**
     * 赋值公共字段
     */
    public void assign(boolean yesCreate){
        if (this instanceof RxBaseDbMust){
            RxBaseDbMust rxBaseDbMust = (RxBaseDbMust) this;
            RxUtil.nullDo(rxBaseDbMust.getValid(),()->rxBaseDbMust.setValid(RX_VALID.NORMAL.getCode()));
            if (yesCreate){
                RxUtil.nullDo(rxBaseDbMust.getVer(),()->rxBaseDbMust.setVer(0L));
            }
        }
        if (this instanceof RxMarkDbMust){
            RxMarkDbMust rxBaseDbMust = (RxMarkDbMust) this;
            String tmpUser="system";
            try{
                tmpUser = RxContentHolder.getUser().getUserInfo().getBid();
            }catch (Exception e){

            }
            final String userId = tmpUser;
            if (yesCreate){
                RxUtil.nullDo(rxBaseDbMust.getCreateById(),()->rxBaseDbMust.setCreateById(userId));
                RxUtil.nullDo(rxBaseDbMust.getCreateDate(),()->rxBaseDbMust.setCreateDate(new Date()));
            }
            RxUtil.nullDo(rxBaseDbMust.getUpdateById(),()->rxBaseDbMust.setUpdateById(userId));
            RxUtil.nullDo(rxBaseDbMust.getUpdateDate(),()->rxBaseDbMust.setUpdateDate(new Date()));
        }
        if (this instanceof RxBizDbMust){
            RxBizDbMust rxBaseDbMust = (RxBizDbMust) this;
            String userName = RxContentHolder.getUser().getUserInfo().getUserName();
            if (yesCreate){
                RxUtil.nullDo(rxBaseDbMust.getCreateBy(),()->rxBaseDbMust.setCreateBy(userName));
                RxUtil.nullDo(rxBaseDbMust.getCreateOrgId(),()->rxBaseDbMust.setCreateOrgId("1"));
                RxUtil.nullDo(rxBaseDbMust.getCreateOrgBy(),()->rxBaseDbMust.setCreateOrgBy("1"));
            }
            RxUtil.nullDo(rxBaseDbMust.getUpdateBy(),()->rxBaseDbMust.setUpdateBy(userName));
        }
    }

    public void assignBatch(boolean yesCreate){
        if (EmptyChecker.isEmpty(dataList)){
            return;
        }
        for (RxBaseData rxBaseData : dataList) {
            rxBaseData.assign(yesCreate);
        }
    }
    /**
     *
     * @return
     */
    public QueryWrapper buildQueryWrapperByCondition(boolean yesUpdate) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (yesUpdate){
            // 主键不更新
            RxBaseDbMust rxBaseDbMust = (RxBaseDbMust) this;
            if (EmptyChecker.notEmpty(rxBaseDbMust.getBid())){
                queryWrapper.eq(RxBaseDbMust.BID,rxBaseDbMust.getBid());
            }

            rxBaseDbMust.setId(null);
            rxBaseDbMust.setBid(null);
        }

        if (EmptyChecker.isEmpty(condition)){
            return queryWrapper;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : condition.entrySet()) {

            // 解析key。对对应的列做对应操作
            int noMatchAnyOperate = 0;
            noMatchAnyOperate += RX_DB_OPERATE.IN.assignQueryWrapper(entry.getKey(),e->queryWrapper.in(e, JSONArray.parseArray(JSONArray.toJSONString(entry.getValue()),String.class)));
            noMatchAnyOperate += RX_DB_OPERATE.GT.assignQueryWrapper(entry.getKey(),e->queryWrapper.gt(e,entry.getValue()));
            noMatchAnyOperate += RX_DB_OPERATE.LT.assignQueryWrapper(entry.getKey(),e->queryWrapper.lt(e,entry.getValue()));
            noMatchAnyOperate += RX_DB_OPERATE.GE.assignQueryWrapper(entry.getKey(),e->queryWrapper.ge(e,entry.getValue()));
            noMatchAnyOperate += RX_DB_OPERATE.LE.assignQueryWrapper(entry.getKey(),e->queryWrapper.le(e,entry.getValue()));
            noMatchAnyOperate += RX_DB_OPERATE.EQ.assignQueryWrapper(entry.getKey(),e->queryWrapper.eq(e,entry.getValue()));
            noMatchAnyOperate += RX_DB_OPERATE.IS_NOT_NULL.assignQueryWrapper(entry.getKey(),e->queryWrapper.isNotNull(e));
            noMatchAnyOperate += RX_DB_OPERATE.IS_NULL.assignQueryWrapper(entry.getKey(),e->queryWrapper.isNull(e));
            noMatchAnyOperate += RX_DB_OPERATE.LIKE_LEFT.assignQueryWrapper(entry.getKey(),e->queryWrapper.likeLeft(e,entry.getValue()));
            noMatchAnyOperate += RX_DB_OPERATE.LIKE_RIGHT.assignQueryWrapper(entry.getKey(),e->queryWrapper.likeRight(e,entry.getValue()));
            noMatchAnyOperate += RX_DB_OPERATE.LIKE.assignQueryWrapper(entry.getKey(),e->queryWrapper.like(e,entry.getValue()));
            noMatchAnyOperate += RX_DB_OPERATE.NOT_IN.assignQueryWrapper(entry.getKey(),e->queryWrapper.notIn(e,JSONArray.parseArray(JSONArray.toJSONString(entry.getValue()),String.class)));
            noMatchAnyOperate += RX_DB_OPERATE.NE.assignQueryWrapper(entry.getKey(),e->queryWrapper.ne(e,entry.getValue()));

            // "sqlGroupby":"FILE_NAME"
            noMatchAnyOperate += RX_DB_OPERATE.SQL.assignQueryWrapper(entry.getKey(),e->{
                stringBuilder.append(RX_DB_OPERATE.MAX.getSql((String) e,(String)entry.getValue()));
                stringBuilder.append(RX_DB_OPERATE.MIN.getSql((String) e,(String)entry.getValue()));
                stringBuilder.append(RX_DB_OPERATE.SUM.getSql((String) e,(String)entry.getValue()));
                stringBuilder.append(RX_DB_OPERATE.AVG.getSql((String) e,(String)entry.getValue()));
                stringBuilder.append(RX_DB_OPERATE.COUNT.getSql((String) e,(String)entry.getValue()));

                if (RX_DB_OPERATE.GROUP.getValue().equalsIgnoreCase((String)e)){
                    queryWrapper.groupBy(RxUtil.StringUtil.humpToLine((String)entry.getValue()).trim());
                    // 用于返回。
                    stringBuilder.append(RX_DB_OPERATE.GROUP.getSql((String) e,(String)entry.getValue()));
                }

                if (RX_DB_OPERATE.ORDER_BY_DESC.getValue().equalsIgnoreCase((String)e)){
                    queryWrapper.orderByDesc(RxUtil.StringUtil.humpToLine((String)entry.getValue()).trim());
                }

                if (RX_DB_OPERATE.ORDER_BY_ASC.getValue().equalsIgnoreCase((String)e)){
                    queryWrapper.orderByAsc(RxUtil.StringUtil.humpToLine((String)entry.getValue()).trim());
                }
            });

            if (noMatchAnyOperate == 0){
                // 没有匹配任何一个
                queryWrapper.eq(RxUtil.StringUtil.humpToLine(entry.getKey()),entry.getValue());
            }
        }
        if (0 < stringBuilder.length()){
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            queryWrapper.select(stringBuilder.toString());
        }
        return queryWrapper;
    }

    public Integer batch(Integer batchSize,boolean yesCreate, ReturnInputMode returnInputMode){
        return batch(getDataList(),batchSize,yesCreate,returnInputMode);
    }

    public Integer batch(List<RxBaseData> dataList,Integer batchSize,boolean yesCreate, ReturnInputMode returnInputMode){
        if (null == batchSize){
            batchSize = Integer.valueOf(1000);
        }
        RxUtil.CheckUtil.condition(EmptyChecker.isEmpty(dataList), RESULT_CODE.MESSAGE,"输入参数失败");
        // 赋值基本用户信息
        assignBatch(yesCreate);
        if(dataList.size() <= batchSize) {
            return (Integer)returnInputMode.handle(dataList);
        }
        Integer integer = 0;
        for (int fromIdx = 0 , endIdx = batchSize ; ; fromIdx += batchSize, endIdx += batchSize) {
            if(endIdx > dataList.size()) {
                endIdx = dataList.size();
            }
            integer += (Integer)returnInputMode.handle(dataList.subList(fromIdx, endIdx));
            if(endIdx == dataList.size()) {
                break;
            }
        }

        return integer;
    }

    public SplitListForSave splitListForSave(){
        RxUtil.CheckUtil.condition(EmptyChecker.isEmpty(getDataList()), RESULT_CODE.MESSAGE,"输入参数失败");
        SplitListForSave splitListForSave = new SplitListForSave();
        for (RxBaseData rxBaseData : getDataList()) {
            RxBaseDbMust baseDbMust = (RxBaseDbMust)rxBaseData;
            if (EmptyChecker.isEmpty(baseDbMust.getBid())){
                splitListForSave.getAddList().add(rxBaseData);
            }else if(RX_VALID.DELETE.getCode().equals(baseDbMust.getValid())){
                splitListForSave.getDeleteList().add(rxBaseData);
            }else {
                splitListForSave.getModifyList().add(rxBaseData);
            }
        }
        return splitListForSave;
    }

    public Map<String,Object> conditionMap(){
        Map<String,Object> map = getCondition();
        if(map == null){
            map = new HashMap<>();
            setCondition(map);
        }
        return map;
    }
    public List<String> conditionList(String key){
        Map<String,Object> map = conditionMap();
        List<String> codeList = (List<String>)map.get(key);
        if(EmptyChecker.isEmpty(codeList)){
            codeList = new ArrayList<>();
            map.put(key,codeList);
        }
        return codeList;
    }
    public void conditionDistinct(String key){
        List<String> codeList =  conditionList(key);
        Set<String> set = new HashSet<>(codeList);
        condition.put(key,new ArrayList<>(set));
    }
}
